//
//  MapNavigationManage.m
//  TongGuanLease
//
//  Created by Henghuitong on 2022/6/28.
//

#import "MapNavigationManage.h"

@implementation MapNavigationManage

+(void)mapNavigationWithLocationLatitude:(NSString* _Nonnull)latitude longitude:(NSString* _Nonnull)longitude addressTitle:(NSString *)addressTitle vc:(UIViewController *)vc
{
    NSMutableArray *mapsA = [NSMutableArray array];
    //苹果原生地图方法和其余不同
    NSMutableDictionary *iosMapDic = [NSMutableDictionary dictionary];
    iosMapDic[@"title"] = @"苹果地图";
    [mapsA addObject:iosMapDic];
    //高德地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        NSMutableDictionary *gaodeMapDic = [NSMutableDictionary dictionary];
        gaodeMapDic[@"title"] = @"高德地图";
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=ios.blackfish.XHY&dlat=%@&dlon=%@&dname=%@&style=2",latitude,longitude,addressTitle] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        gaodeMapDic[@"url"] = urlString;
        [mapsA addObject:gaodeMapDic];
    }
    //百度地图
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        NSMutableDictionary *baiduMapDic = [NSMutableDictionary dictionary];
        baiduMapDic[@"title"] = @"百度地图";

//        CLLocationCoordinate2D location = [JZLocationConverter gcj02ToBd09:endLocation];
//        NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name:%@&coord_type=gcj02&mode=driving&src=ios.blackfish.XHY",location.latitude,location.longitude,titleStr] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
           NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%@,%@|name:%@&coord_type=gcj02&mode=driving&src=ios.blackfish.XHY",latitude,longitude,addressTitle] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        
        baiduMapDic[@"url"] = urlString;
        [mapsA addObject:baiduMapDic];
    
    //腾讯地图
   //    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
   //        NSMutableDictionary *qqMapDic = [NSMutableDictionary dictionary];
   //        qqMapDic[@"title"] = @"腾讯地图";
   //        NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&to=%@&tocoord=%f,%f&coord_type=1&referer={ios.blackfish.XHY}",titleStr,endLocation.latitude,endLocation.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
   //        qqMapDic[@"url"] = urlString;
   //        [mapsA addObject:qqMapDic];
   //      }

    }
    
    //手机地图个数判断
    if (mapsA.count > 0) {
        //选择
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"使用导航" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        NSInteger index = mapsA.count;
        
        for (int i = 0; i < index; i++) {
            
            NSString *title = mapsA[i][@"title"];
            NSString *urlString = mapsA[i][@"url"];
            if (i == 0) {
                
                UIAlertAction *iosAntion = [UIAlertAction actionWithTitle:title style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                   
                    CLLocationCoordinate2D customLoc2D = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
    
          
                    [self appleNaiWithCoordinate:customLoc2D andWithMapTitle:addressTitle];
                    
                }];
                [alertVC addAction:iosAntion];
                continue;
            }
            
            UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
            }];
            
            [alertVC addAction:action];
        }
        
        UIAlertAction *cancleAct = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertVC addAction:cancleAct];

        [vc presentViewController:alertVC animated:YES completion:^{
            
        }];
    }else{
        NSLog(@"未检测到地图应用");
        [TGProgressHUD showMBHUDErrorTip:@"未检测到地图应用"];
    }
}


//唤醒苹果自带导航
+(void)appleNaiWithCoordinate:(CLLocationCoordinate2D)coordinate andWithMapTitle:(NSString *)map_title{
  
    MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *tolocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
    tolocation.name = map_title;
    [MKMapItem openMapsWithItems:@[currentLocation,tolocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
                                                                               MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}


@end
